//
//  CLImageShowView.m
//  CLImagePicker
//
//  Created by Criss on 2017/3/17.
//  Copyright © 2017年 Criss. All rights reserved.
//

#import "CLImageShowView.h"

@implementation CLImageShowView {
    UIImageView *_imageView;
    CGRect _originalRect;
}

- (instancetype)initWithImageView:(UIImageView *)imageView converFrame:(CGRect)frame {
    if (self = [super init]) {
        _imageView = [[UIImageView alloc] initWithImage:imageView.image];
        _imageView.frame = frame;
        [self showUI];
    }
    return self;
}

- (void)showUI {
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.7];
}

- (void)didMoveToSuperview {
    [self addSubview:_imageView];
    [UIView animateWithDuration:0.4 animations:^{
        CGRect rect = _imageView.frame;
        _originalRect = rect;
        rect.size.width = [UIScreen mainScreen].bounds.size.width;
        rect.size.height = rect.size.width / _imageView.image.size.width * _imageView.image.size.height;
        _imageView.frame = rect;
        _imageView.center = self.center;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [UIView animateWithDuration:0.4 animations:^{
        _imageView.frame = _originalRect;
        self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0];
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
